﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;

using NGE;

namespace MH
{
    public class ScriptComponent:Component
    {
        public ScriptComponent(string name, Game g)
            : base(name, g)
        {
            File.Delete("script_log.txt");
            SetValue("pc_name", "null");
            SetValue("menu_return", "null");
            SetValue("bg_music", "null");
            SetValue("playing_music", "null");
        }

        SceneComponent sceneCOM
        {
            get
            {
                return ((SceneComponent)game.GetComponent("SceneCOM"));
            }
        }

        void execute(string cmd)
        {
            string[] words = cmd.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries);
            //words[0] = words[0].ToUpper();
            //for (int i = 0; i < words.Length; i++)
            //{
            //    words[i] = words[i].ToUpper();
            //}
            //if (vars.ContainsKey(words[0]))
            //{

            //}
            //else
            do
            {
                //
                for (int i = 0; i < words.Length; i++)
                {
                    //if (words[i] == "then")
                    //{
                    //    break;
                    //}
                    File.AppendAllText("script_log.txt", words[i] + " ");
                }
                File.AppendAllText("script_log.txt", "\r\n");
                //

                repeat = false;
                if (then)
                {
                    int ti = then_index(words);
                    if (ti < 0)
                    {
                        then = false;
                    }
                    else
                    {
                        words = update_words(words, ti + 1);
                        //repeat = true;
                    }
                }
                #region if
                if (words[0].ToUpper() == "IF")
                {
                    then = false;
                    object value = GetValue(words[1]);
                    if (words[2] == "=")
                    {
                        if ((string)value == (words[3]))
                        {
                            then = true;
                        }
                    }
                    else if (words[2] == "#")
                    {
                        if (int.Parse(words[1]) != int.Parse(words[3]))
                        {
                            then = true;
                        }
                    }
                    else if (words[2] == "<")
                    {
                        if (int.Parse(words[1]) < int.Parse(words[3]))
                        {
                            then = true;
                        }
                    }
                    else if (words[2] == "<=")
                    {
                        if (int.Parse(words[1]) <= int.Parse(words[3]))
                        {
                            then = true;
                        }
                    }
                    else if (words[2] == ">")
                    {
                        if (int.Parse(words[1]) > int.Parse(words[3]))
                        {
                            then = true;
                        }
                    }
                    else if (words[2] == ">=")
                    {
                        if (int.Parse(words[1]) >= int.Parse(words[3]))
                        {
                            then = true;
                        }
                    }
                }
                #endregion
                #region roll
                else if (words[0].ToUpper() == "ROLL")
                {
                    int low = int.Parse(words[1]);
                    int high = int.Parse(words[2]);
                    int v = Util.Roll(low, high);
                    SetValue(words[3], v.ToString());
                }
                #endregion
                #region battle
                else if (words[0].ToUpper() == "BATTLE")
                {
                    if (words[1].ToUpper() == "IN")
                    {
                        sceneCOM.IsInBattle = true;
                    }
                    else if (words[1].ToUpper() == "OUT")
                    {
                        sceneCOM.IsInBattle = false;
                    }
                    else if (words[1].ToUpper() == "BUSY")
                    {
                        sceneCOM.IsFighting = true;
                    }
                    else if (words[1].ToUpper() == "NOTBUSY")
                    {
                        sceneCOM.IsFighting = false;
                    }
                }
                #endregion
                #region shock
                else if (words[0].ToUpper() == "SHOCK")
                {
                    int time = int.Parse(words[1]);
                    game.GetComponent("SceneCOM").ShockScreen(time);
                }
                #endregion
                #region delay
                else if (words[0].ToUpper() == "DELAY")
                {
                    int len = int.Parse(words[1]);
                    Delay(len, true);
                }
                #endregion
                #region fallout
                else if (words[0].ToUpper() == "FALLOUT")
                {
                    int time = int.Parse(words[1]);
                    game.GetComponent("SceneCOM").FallOutSceen(time);
                }
                #endregion
                #region fallin
                else if (words[0].ToUpper() == "FALLIN")
                {
                    int time = int.Parse(words[1]);
                    game.GetComponent("SceneCOM").FallInSceen(time);
                }
                #endregion
                #region setctrl
                else if (words[0].ToUpper() == "SETCTRL")
                {
                    sceneCOM.Controllable = true;
                }
                #endregion
                #region clrctrl
                else if (words[0].ToUpper() == "CLRCTRL")
                {
                    sceneCOM.Controllable = false;
                }
                #endregion
                #region loadchr
                else if (words[0].ToUpper() == "LOADCHR")
                {
                    Character chr = (Character)Util.LoadObject(words[1]);
                    sceneCOM.CHRs.Add(chr);
                    if (chr.Type == SpriteType.PC)
                    {
                        SetValue("pc_name", chr.Name);
                    }
                }
                #endregion
                #region focus
                else if (words[0].ToUpper() == "FOCUS")
                {
                    int x = (int)double.Parse(words[1]);
                    int y = (int)double.Parse(words[2]);
                    sceneCOM.SceneFocusAt(new Microsoft.DirectX.Vector3(x, y, 0));
                }
                #endregion
                #region msg
                else if (words[0].ToUpper() == "MSG")
                {
                    if (words.Length == 1)
                    {
                        game.MenuCOM.Close("信息");
                    }
                    else
                    {
                        int x = 0;
                        int y = 0;
                        string txt = "";
                        try
                        {
                            x = int.Parse(words[1]);
                            y = int.Parse(words[2]);
                            words[3] = words[3].Replace("/n", "\n");
                            words[3] = words[3].Replace("/s", " ");
                            txt = words[3];
                        }
                        catch
                        {
                            words[1] = words[1].Replace("/n", "\n");
                            words[1] = words[1].Replace("/s", " ");
                            txt = words[1];
                        }
                        game.MenuCOM.Open("信息", new Microsoft.DirectX.Vector3(x, y, 0), txt);
                    }
                }
                #endregion
                #region var
                else if (words[0].ToUpper() == "VAR")
                {
                    try
                    {
                        SetValue(words[1], words[2]);
                    }
                    catch
                    {
                        SetValue(words[1], "null");
                    }
                }
                #endregion
                #region full
                else if (words[0].ToUpper() == "FULL")
                {
                    game.GoFullScreen();
                }
                #endregion
                #region block
                else if (words[0].ToUpper() == "BLOCK")
                {
                    #region key
                    if (words[1] == "keyup")
                    {
                        if (words[2] == "any")
                        {
                            keycode = -1;
                            isBlocking = true;
                            keyup = false;
                        }
                        else
                        {
                            keycode = int.Parse(words[2]);
                            isBlocking = true;
                            keyup = false;
                        }
                    }
                    #endregion
                    else if (ValueExist(words[1]))
                    {
                        block_var = words[1];
                        block_val = words[3];
                        if (words[2] == "=")
                        {
                            block_type = 0;
                        }
                        else if (words[2] == "#")
                        {
                            block_type = 1;
                        }
                        isBlocking = true;
                    }
                    else if (sceneCOM.GetCHRIndex(words[1]) > -1)
                    {
                        int i = sceneCOM.GetCHRIndex(words[1]);

                        if (words[2].ToUpper() == "NOTHING")
                        {
                            block_var = words[1];
                            block_val = words[2];
                            isBlocking = true;
                        }
                    }
                    else if (words[1].StartsWith("#"))
                    {
                        words[1] = words[1].Substring(1);
                        if (ValueExist(words[1]))
                        {
                            //string varname = words[0].Substring(1);
                            words[1] = (string)GetValue(words[1]);

                            int i = sceneCOM.GetCHRIndex(words[1]);

                            if (words[2].ToUpper() == "NOTHING")
                            {
                                block_var = words[1];
                                block_val = words[2];
                                isBlocking = true;
                            }
                        }
                    }
                }
                #endregion
                #region win
                else if (words[0].ToUpper() == "WIN")
                {
                    game.GoWindowed();
                }
                #endregion
                #region exit
                else if (words[0].ToUpper() == "EXIT")
                {
                    game.Exit();
                }
                #endregion
                #region ani
                else if (words[0].ToUpper() == "ANI")
                {
                    string regName = words[1];
                    //bool autoclr = false;
                    //bool isflip = false;
                    int times = -1;

                    try
                    {
                        times = int.Parse(words[3]);
                    }
                    catch { }

                    if (words[4].ToUpper() == "AUTOCLR")
                    {
                        //autoclr = true;
                    } 
                    if (words[5].ToUpper() == "FLIP")
                    {
                        //isflip = true;
                    }

                    int fromx = int.Parse(words[6]);
                    int fromy = int.Parse(words[7]);
                    int dly = 1;
                    try
                    {
                        dly = int.Parse(words[10]);
                    }
                    catch { }
                    
                    try
                    {
                        int tox = int.Parse(words[8]);
                        int toy = int.Parse(words[9]);
                        //game.AnimationCOM.DrawAnimation(regName, words[2], new Microsoft.DirectX.Vector3(fromx, fromy, 0), new Microsoft.DirectX.Vector3(tox, toy, 0), times, autoclr, dly, isflip);
                    }
                    catch
                    {
                        //game.AnimationCOM.DrawAnimation(regName, words[2], new Microsoft.DirectX.Vector3(fromx, fromy, 0), times, autoclr, isflip);
                    }
                }
                #endregion
                #region music
                else if (words[0].ToUpper() == "MUSIC")
                {
                    game.Music.IsLoop = false;
                    if (words[1].ToUpper() == "LOOP")
                    {
                        game.Music.Load(words[2]);
                        game.Music.IsLoop = true;
                        game.Music.Play();
                        SetValue("playing_music", words[2]);
                    }
                    else if (words[1].ToUpper() == "OUT")
                    {
                        game.Music.Stop();
                    }
                    else if (words[1].ToUpper() == "RESTORE")
                    {
                        game.Music.Volume = music_mem;
                    }
                    else if (words[1].ToUpper() == "VOL")
                    {
                        int v = int.Parse(words[2]);
                        music_mem = game.Music.Volume;
                        game.Music.Volume = v;
                    }
                    else
                    {
                        game.Music.Load(words[1]);
                        game.Music.Play();
                        SetValue("playing_music", words[1]);
                    }
                }
                #endregion
                #region effect
                else if (words[0].ToUpper() == "EFFECT")
                {
                    game.Effect.IsLoop = false;
                    if (words[1].ToUpper() == "LOOP")
                    {
                        game.Effect.Load(words[2]);
                        game.Effect.IsLoop = true;
                        game.Effect.Play();
                    }
                    else if (words[1].ToUpper() == "OUT")
                    {
                        game.Effect.Stop();
                    }
                    else if (words[1].ToUpper() == "RESTORE")
                    {
                        game.Effect.Volume = effect_mem;
                    }
                    else if (words[1].ToUpper() == "VOL")
                    {
                        int v = int.Parse(words[2]);
                        effect_mem = game.Effect.Volume;
                        game.Effect.Volume = v;
                    }
                    else
                    {
                        game.Effect.Load(words[1]);
                        game.Effect.Play();
                    }
                }
                #endregion
                #region speech
                else if (words[0].ToUpper() == "SPEECH")
                {
                    game.Speech.IsLoop = false;
                    if (words[1].ToUpper() == "LOOP")
                    {
                        game.Speech.Load(words[2]);
                        game.Speech.IsLoop = true;
                        game.Speech.Play();
                    }
                    else if (words[1].ToUpper() == "OUT")
                    {
                        game.Speech.Stop();
                    }
                    else if (words[1].ToUpper() == "RESTORE")
                    {
                        game.Speech.Volume = speech_mem;
                    }
                    else if (words[1].ToUpper() == "VOL")
                    {
                        int v = int.Parse(words[2]);
                        speech_mem = game.Speech.Volume;
                        game.Speech.Volume = v;
                    }
                    else
                    {
                        game.Speech.Load(words[1]);
                        game.Speech.Play();
                    }
                }
                #endregion
                #region menu
                else if (words[0].ToUpper() == "MENU")
                {
                    if (words[1].ToUpper() == "OPEN")
                    {
                        string name = null;
                        if (words.Length == 6)
                        {
                            name = words[5];
                            name = name.Replace("/n", "\n");
                        }
                        else if (words.Length == 4)
                        {
                            name = words[3];
                            name = name.Replace("/n", "\n");
                        }
                        int x = 0;
                        int y = 0;
                        try
                        {
                            x = int.Parse(words[3]);
                            y = int.Parse(words[4]);

                            game.MenuCOM.Open(words[2], new Microsoft.DirectX.Vector3(x, y, 0), name);
                        }
                        catch
                        {
                            game.MenuCOM.Open(words[2], name);
                        }
                    }
                    else if (words[1].ToUpper() == "REFRESH")
                    {
                        if (words.Length == 4)
                        {
                            game.MenuCOM.Refresh(words[2], words[3]);
                        }
                        else
                        {
                            game.MenuCOM.Refresh(words[2], null);
                        }
                    }
                    else if (words[1].ToUpper() == "CLOSE")
                    {
                        game.MenuCOM.Close();
                        //isBlocking = false;
                    }
                    //else if (words[1] == "return")
                    //{
                    //    SetValue("menu_return", words[2]);
                    //}
                }
                #endregion
                #region 精灵脚本
                else
                {
                    bool ismon=false;
                    if (words[0].StartsWith("#"))
                    {
                        words[0] = words[0].Substring(1);
                        if (ValueExist(words[0]))
                        {
                            //string varname = words[0].Substring(1);
                            words[0] = (string)GetValue(words[0]);
                        }
                    }
                    int chrI = sceneCOM.GetCHRIndex(words[0]);
                    if(chrI<0)
                    {
                        chrI=sceneCOM.GetMONIndex(words[0]);
                        ismon=true;
                    }
                    if (chrI > -1)
                    {
                        Character chr;
                        if (ismon == false)
                        {
                            chr = sceneCOM.CHRs[chrI];
                        }
                        else
                        {
                            chr = sceneCOM.MonstersInBattle[chrI];
                        }

                        if (words[0].ToLower() == chr.Name.ToLower())
                        {
                            #region say
                            if (words[1].ToUpper() == "SAY")
                            {
                                if (words.Length > 2)
                                {
                                    words[2] = words[2].Replace("/n", "\n");

                                    Microsoft.DirectX.Vector3 loc = Util.Vector3AddVector3(sceneCOM.SCN.Location, chr.Location);
                                    System.Drawing.Size sz = Util.GetTextSize(words[2], game.Option.Font.Height);

                                    loc.Y -= sz.Height;
                                    loc.X -= sz.Width / 2;

                                    game.MenuCOM.Open("信息", loc, words[2]);
                                }
                                else
                                {
                                    game.MenuCOM.Close("信息");
                                }
                            }
                            #endregion
                            #region avatar
                            else if (words[1].ToUpper() == "AVATAR")
                            {
                                if (words[2].ToUpper() == "STAND")
                                {
                                    if (chr.Avatar == null)
                                    {
                                        return;
                                    }
                                    chr.AvatarAct = CharacterAvaterActs.Stand;
                                }
                                else if (words[2].ToUpper() == "READY")
                                {
                                    if (chr.AvatarReady == null)
                                    {
                                        return;
                                    }
                                    chr.AvatarAct = CharacterAvaterActs.Ready;
                                }
                                else if (words[2].ToUpper() == "FIGHT")
                                {
                                    if (chr.AvatarFight == null)
                                    {
                                        return;
                                    }
                                    chr.AvatarAct = CharacterAvaterActs.Fight;
                                }
                                else if (words[2].ToUpper() == "HIT")
                                {
                                    if (chr.AvatarHit == null)
                                    {
                                        return;
                                    }
                                    chr.AvatarAct = CharacterAvaterActs.Hit;
                                }
                            }
                            #endregion
                            #region enter
                            else if (words[1].ToUpper() == "ENTER")
                            {
                                Scene scn = sceneCOM.LoadScene(words[2]);
                                sceneCOM.EnterScene(scn);
                                int x = int.Parse(words[3]);
                                int y = int.Parse(words[4]);
                                Direction dir = Util.GetDirection(words[5]);
                                sceneCOM.SetCHR(chr, x, y, dir);
                                sceneCOM.SceneFocusAt(new Microsoft.DirectX.Vector3(x, y, 0));
                            }
                            #endregion
                            #region dir
                            else if (words[1].ToUpper() == "DIR")
                            {
                                Direction dir = Util.GetDirection(words[2]);
                                chr.Direction = dir;
                            }
                            #endregion
                            #region isfocus
                            else if (words[1].ToUpper() == "ISFOCUS")
                            {
                                int x = (int)chr.Location.X / sceneCOM.SCN.TileSize.Width;
                                int y = (int)chr.Location.Y / sceneCOM.SCN.TileSize.Height;
                                sceneCOM.SceneFocusAt(new Microsoft.DirectX.Vector3(x, y, 0));
                            }
                            #endregion
                            #region hp+
                            else if (words[1].ToUpper() == "HP+")
                            {
                                chr.HP += int.Parse(words[2]);
                                if (chr.HP > chr.HPMax)
                                {
                                    chr.HP = chr.HPMax;
                                }
                            }
                            #endregion
                            #region hp-
                            else if (words[1].ToUpper() == "HP-")
                            {
                                chr.HP -= int.Parse(words[2]);
                                if (chr.HP > chr.HPMax)
                                {
                                    chr.HP = chr.HPMax;
                                }
                            }
                            #endregion
                            #region use
                            else if (words[1].ToUpper() == "USE")
                            {
                                chr.Use(int.Parse(words[2]));
                            }
                            #endregion
                            #region drop
                            else if (words[1].ToUpper() == "DROP")
                            {
                                chr.Drop(int.Parse(words[2]));
                            }
                            #endregion
                            #region dropspoil
                            else if (words[1].ToUpper() == "DROPSPOIL")
                            {
                                chr.Spoils.Clear();
                            }
                            #endregion
                            #region unequip
                            else if (words[1].ToUpper() == "UNEQUIP")
                            {
                                int pt = int.Parse(words[2]);
                                chr.Unequip(pt);
                            }
                            #endregion
                            #region palsy
                            else if (words[1].ToUpper() == "PALSY")
                            {
                                chr.IsPalsy = true;
                            }
                            #endregion
                            #region notpalsy
                            else if (words[1].ToUpper() == "NOTPALSY")
                            {
                                chr.IsPalsy = false;
                            }
                            #endregion
                            #region move
                            else if (words[1].ToUpper() == "MOVE")
                            {
                                int times = 1;
                                try
                                {
                                    times = int.Parse(words[3]);
                                }
                                catch { }
                                Datagram.PlayerCharacterMoveInfo pcmi;

                                pcmi.Name = words[0];
                                pcmi.MoveSpeed = chr.MoveSpeed;
                                pcmi.Steps = 1;

                                if (words[2].ToUpper() == "U")
                                {
                                    pcmi.Direction = Direction.U;
                                    for (int j = 0; j < times; j++)
                                    {
                                        chr.Move(pcmi);
                                    }
                                }
                                else if (words[2].ToUpper() == "L")
                                {
                                    pcmi.Direction = Direction.L;
                                    for (int j = 0; j < times; j++)
                                    {
                                        chr.Move(pcmi);
                                    }
                                }
                                else if (words[2].ToUpper() == "D")
                                {
                                    pcmi.Direction = Direction.D;
                                    for (int j = 0; j < times; j++)
                                    {
                                        chr.Move(pcmi);
                                    }
                                }
                                else if (words[2].ToUpper() == "R")
                                {
                                    pcmi.Direction = Direction.R;
                                    for (int j = 0; j < times; j++)
                                    {
                                        chr.Move(pcmi);
                                    }
                                }
                            }
                            #endregion
                            #region fly
                            else if (words[1].ToUpper() == "FLY")
                            {
                                int x = int.Parse(words[2]);
                                int y = int.Parse(words[3]);
                                Direction dir = Util.GetDirection(words[4]);
                                sceneCOM.SetCHR(chr, x, y, dir);
                            }
                            #endregion
                            #region hide
                            else if (words[1].ToUpper() == "HIDE")
                            {
                                chr.IsHide = true;
                            }
                            #endregion
                            #region show
                            else if (words[1].ToUpper() == "SHOW")
                            {
                                chr.IsHide = false;
                            }
                            #endregion
                            #region rdir
                            else if (words[1].ToUpper() == "RDIR")
                            {
                                chr.RestoreDirection();
                            }
                            #endregion
                            #region bagin
                            else if (words[1].ToUpper() == "BAGIN")
                            {
                                
                                Prop prop = sceneCOM.Props[words[2]];
                                chr.BagIN(prop);

                                //Microsoft.DirectX.Vector3 loc;

                                //loc.X = 10;
                                //loc.Y = 190;

                                //string cmd2 = "";
                                //cmd2 += "msg " + loc.X + " " + loc.Y + " " + chr.Name + "得到了" + prop.Name + "！\n";
                                //cmd2 += "delay 800\n";
                                //cmd2 += "msg\n";

                                //IS(cmd2);
                            }
                            #endregion
                            #region bagout
                            else if (words[1].ToUpper() == "BAGOUT")
                            {
                                int index = int.Parse(words[2]);
                                chr.BagOUT(index);
                            }
                            #endregion
                            #region g-
                            else if (words[1].ToUpper() == "G-")
                            {
                                int index = int.Parse(words[2]);
                                chr.G -= index;
                            }
                            #endregion
                            #region g+
                            else if (words[1].ToUpper() == "G+")
                            {
                                int index = int.Parse(words[2]);
                                chr.G += index;
                            }
                            #endregion
                            #region attack
                            else if (words[1].ToUpper() == "ATTACK")
                            {
                                //chr.BattleAct = CharacterActs.BattleFight;
                                int x = 0;
                                int y = 0;

                                string cmd2 = "";

                                Character tchr = sceneCOM.GetCHR(words[2]);
                                Microsoft.DirectX.Vector3 loc = Util.Vector3AddVector3(tchr.Location, sceneCOM.SCN.Location);
                                loc.Y -= game.Option.Font.Height;

                                loc.X = 10;
                                loc.Y = 190;

                                Direction dir = Util.GetDirection(chr.Location, tchr.Location);
                                Direction dir2 = Util.GetDirection(tchr.Location, chr.Location);

                                if (tchr.Location.X > chr.Location.X)
                                {
                                    chr.AvatarFlip = true;
                                }

                                if (chr.Weapon.Name == string.Empty)
                                {

                                    cmd2 += chr.Name + " dir " + dir.ToString() + "\n";
                                    cmd2 += tchr.Name + " dir " + dir2.ToString() + "\n";

                                    cmd2 += chr.Name + " avatar fight\n";
                                    cmd2 += "effect data\\sound\\effect\\fist.mp3\n";
                                    cmd2 += "delay 200\n";

                                    if (dir2 == Direction.L || tchr.Direction == Direction.R)
                                    {
                                        cmd2 += tchr.Name + " avatar hit\n";  
                                    }
                                    cmd2 += "effect data\\sound\\effect\\fisthit.mp3\n";
                                    cmd2 += "delay 200\n";
                                    cmd2 += chr.Name + " avatar stand\n";
                                }
                                else
                                {
                                    string isflip = "noflip ";
                                    if (chr.AvatarFlip)
                                    {
                                        isflip = "flip ";
                                    }

                                    cmd2 += chr.Name + " dir " + dir.ToString() + "\n";
                                    cmd2 += tchr.Name + " dir " + dir2.ToString() + "\n";

                                    cmd2 += chr.Name + " avatar weapon\n";

                                    x = (int)chr.Location.X + (int)sceneCOM.SCN.Location.X;
                                    y = (int)chr.Location.Y + (int)sceneCOM.SCN.Location.Y;

                                    if (chr.AvatarFlip)
                                    {
                                        x += sceneCOM.SCN.TileSize.Width - sceneCOM.SCN.TileSize.Width / 5 - 2;
                                    }
                                    else
                                    {
                                        x += sceneCOM.SCN.TileSize.Width / 5;
                                    }

                                    y += sceneCOM.SCN.TileSize.Height / 3 * 2;


                                    cmd2 += "ani " + chr.Name + "weaponskin " + game.Option.AnimationPath + chr.Weapon.SkinFileName + " 1 autoclr " + isflip + x + " " + y + "\n";

                                    cmd2 += "effect " + game.Option.SoundPath + chr.Weapon.SkinSoundFileName + "\n";
                                    cmd2 += "delay 200\n";

                                    if (dir2 == Direction.L || tchr.Direction == Direction.R)
                                    {
                                        cmd2 += tchr.Name + " avatar hit\n";
                                    }
                                    //cmd2 += "effect data\\sound\\effect\\fisthit.mp3\n";
                                    //cmd2 += "delay 200\n";

                                    x = (int)tchr.Location.X + (int)sceneCOM.SCN.Location.X;
                                    y = (int)tchr.Location.Y + (int)sceneCOM.SCN.Location.Y;

                                    if (chr.AvatarFlip)
                                    {
                                        x += sceneCOM.SCN.TileSize.Width - sceneCOM.SCN.TileSize.Width / 2 - 1;
                                    }
                                    else
                                    {
                                        x += sceneCOM.SCN.TileSize.Width / 2;
                                    }

                                    y += sceneCOM.SCN.TileSize.Height;

                                    cmd2 += "ani " + chr.Name + "weaponhit " + game.Option.AnimationPath + chr.Weapon.HitFileName + " 1 autoclr " + isflip + x + " " + y + "\n";

                                    cmd2 += "effect " + game.Option.SoundPath + chr.Weapon.HitSoundFileName + "\n";
                                    cmd2 += "delay 200\n";
                                    cmd2 += chr.Name + " avatar stand\n";
                                }
                                if (Algo.MissHit(chr, tchr))
                                {
                                    cmd2 += "msg " + loc.X + " " + loc.Y + " " + chr.Name + "攻击" + tchr.Name + "/n没打中\n";
                                    cmd2 += "delay 1000\n";
                                    cmd2 += "msg\n";
                                    IS(cmd2);
                                }
                                else
                                {

                                    double dmg = Algo.Damage(chr, tchr);

                                    if (dmg > 0)
                                    {
                                        cmd2 += tchr.Name + " hp- " + dmg + "\n";
                                        //tchr.HP -= (int)dmg;

                                        cmd2 += "msg " + loc.X + " " + loc.Y + " " + chr.Name + "攻击" + tchr.Name + "/n造成" + (int)dmg + "点伤害\n";
                                        cmd2 += "delay 1000\n";
                                        cmd2 += "msg\n";

                                        if (tchr.HP - dmg < 0)
                                        {
                                            tchr.HP = 0;
                                            cmd2 += "msg " + loc.X + " " + loc.Y + " " + chr.Name + "消灭了" + tchr.Name + "！\n";
                                            cmd2 += "delay 1000\n";
                                            cmd2 += "msg\n";
                                        }

                                        IS(cmd2);
                                    }
                                    else
                                    {
                                        cmd2 += "msg " + loc.X + " " + loc.Y + " " + chr.Name + "攻击" + tchr.Name + "/n没有造成伤害\n";
                                        cmd2 += "delay 1000\n";
                                        cmd2 += "msg\n";
                                        IS(cmd2);
                                    }
                                }
                            }
                            #endregion
                            #region check
                            else if (words[1].ToUpper() == "CHECK")
                            {
                                string cmd2 = "";
                                Character tchr = sceneCOM.GetCHR(chr.Location,chr.Name);

                                if (tchr != null)
                                {
                                    if (tchr.Type == SpriteType.Box)
                                    {
                                    }
                                    else if (tchr.Type == SpriteType.Monster)
                                    {
                                        if (tchr.HP < 1)
                                        {
                                            if (tchr.Spoils != null)
                                            {
                                                if (tchr.Spoils.Count > 0)
                                                {
                                                    foreach (Spoil s in tchr.Spoils)
                                                    {
                                                        int seed = Util.Roll(1, 10000);
                                                        bool can = Util.RollBool();
                                                        if (can)
                                                        {
                                                            if (seed >= s.Seed)
                                                            {
                                                                Microsoft.DirectX.Vector3 loc;

                                                                loc.X = 10;
                                                                loc.Y = 190;
                                                               
                                                                cmd2 += "msg " + loc.X + " " + loc.Y + " " + chr.Name + "找到了" + s.Name + "。\n";
                                                                cmd2 += "delay 1000\n";
                                                                cmd2 += "msg\n";                                                               
                                                                cmd2 += chr.Name + " bagin " + s.Name + "\n";                                                               
                                                            }
                                                        }
                                                    }
                                                    cmd2 += tchr.Name + "dropspoil\n";
                                                }
                                            }
                                        }
                                    }
                                } 
                                IS(cmd2);
                            }
                            #endregion
                            #region driver
                            else if (words[1].ToUpper() == "DRIVE")
                            {
                                if (chr.DrivingMetal!=null)
                                {
                                    chr.ExitMetal();
                                }
                                else
                                {
                                    chr.EnterMetal();
                                }
                            }
                            #endregion
                        }
                    }
                }
                #endregion
            }
            while (repeat);
        }
        public void CL()
        {
            cmdque.Clear();
            cmdqueblock.Clear();
        }
        public void AppendNScript(string fileName)
        {
            if (System.IO.Path.GetExtension(fileName) == ".nspt")
            {
                AS(Util.Decrypt(System.IO.File.ReadAllBytes(fileName)));
            }
        }
        public void AS(string script)
        {
            AppendScript(script, true);
        }
        public void AS(string script, bool block)
        {
            AppendScript(script, block);
        }
        public void AppendScript(string script, bool block)
        {
            if (script == null)
            {
                return;
            }
            string[] lines = script.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in lines)
            {
                if (block)
                {
                    cmdqueblock.Add(line);
                }
                else
                {
                    cmdque.Add(line);
                }
            }
        }

        public void IS(string script)
        {
            InsertScript(script, true);
        }
        public void IS(string script, bool block)
        {
            InsertScript(script, block);
        }
        public void InsertScript(string script, bool block)
        {
            if (script == null)
            {
                return;
            }
            string[] lines = script.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            int i = 0;
            foreach (string line in lines)
            {
                if (block)
                {
                    cmdqueblock.Insert(i, line);
                }
                else
                {
                    cmdque.Insert(i, line);
                }
                i++;
            }
        }     
        public override void OnKeyboardEventCode(object sender, int key, KeyboardState ks)
        {
            if (ks == KeyboardState.KeyDown)
            {
                if (!keydown)
                {
                    if (keycode == -1)
                    {
                        keydown = true;
                        isBlocking = false;
                    }
                    else if (keycode == key)
                    {
                        keydown = true;
                        isBlocking = false;
                    }
                }
            }
            else if (ks == KeyboardState.KeyHold)
            {
                if (!keyhold)
                {
                    if (keycode == -1)
                    {
                        keyhold = true;
                        isBlocking = false;
                    }
                    else if (keycode == key)
                    {
                        keyhold = true;
                        isBlocking = false;
                    }
                }
            }
            else if (ks == KeyboardState.KeyUp)
            {
                if (!keyup)
                {
                    if (keycode == -1)
                    {
                        keyup = true;
                        isBlocking = false;
                    }
                    else if (keycode == key)
                    {
                        keyup = true;
                        isBlocking = false;
                    }
                }
            }
        }

        bool keydown = true;
        bool keyup = true;
        bool keyhold = true;
        int keycode;

        bool isBlocking;
        
        //Game game;
        //bool isDelaying
        //{
        //    get
        //    {
        //        if (game.RunningTotalSeconds - delayStartTime > delayDuration)
        //        {
        //            return false;
        //        }
        //        return true;
        //    }
        //}
        //double delayStartTime;
        //double delayDuration = 0;
        //void delay(int msec)
        //{
        //    if (isDelaying)
        //    {
        //        delayDuration += msec / 1000.0;
        //    }
        //    else
        //    {
        //        delayStartTime = game.RunningTotalSeconds;
        //        delayDuration = msec / 1000.0;
        //    }
        //}
        public override void Heartbeat()
        {
            //base.Heartbeat();       
            //if (isDelaying)
            //{
            //    return;
            //}
            if (cmdque.Count > 0)
            {
                string cmd = cmdque[0];
                cmdque.RemoveAt(0);
                execute(cmd);
                //return;
            }
            if (isBlocking)
            {
                if (sceneCOM.GetCHRIndex(block_var) > -1)
                {
                    int i = sceneCOM.GetCHRIndex(block_var);
                    if (block_val == "nothing")
                    {
                        if (sceneCOM.CHRs[i].IsNothing)
                        {
                            isBlocking = false;
                            block_var = "null";
                            block_val = "null";
                        }
                    }
                }
                if (block_type == 0)
                {
                    string value = GetValue(block_var);
                    if (value == block_val)
                    {
                        isBlocking = false;
                        block_type = -1;
                    }
                }
                else if (block_type == 1)
                {
                    string value = GetValue(block_var);
                    if (value != block_val)
                    {
                        isBlocking = false;
                        block_type = -1;
                    }
                }
                return;
            }
            if (cmdqueblock.Count > 0)
            {
                string cmd = cmdqueblock[0];
                cmdqueblock.RemoveAt(0);
                execute(cmd);
            }
        }
        

        #region for script
        Hashtable vars = new Hashtable();
        public string GetValue(string key)
        {
            if (key == null)
            {
                return "null";
            }
            if (vars.ContainsKey(key))
            {
                return (string)vars[key];
            }
            return "null";
        }
        public void SetValue(string key, string value)
        {
            if (vars.ContainsKey(key) == false)
            {
                vars.Add(key, value);
            }
            else
            {
                vars[key] = value;
            }
        }
        public bool ValueExist(string key)
        {
            if (vars.ContainsKey(key))
            {
                return true;
            }
            return false;
        }

        List<string> cmdque = new List<string>();
        List<string> cmdqueblock = new List<string>();

        string block_var;
        string block_val;
        int block_type;
        bool then;
        bool repeat;

        int music_mem;
        int effect_mem;
        int speech_mem;

        int get_then_count(string[] wordss)
        {
            int j = 0;
            for (int i = 0; i < wordss.Length; i++)
            {
                if (wordss[i] == "then")
                {
                    j++;
                }
            }
            return j;
        }
        int then_index(string[] wordss)
        {
            for (int i = 0; i < wordss.Length; i++)
            {
                if (wordss[i] == "then")
                {
                    return i;
                }
            }
            return -1;
        }
        string[] update_words(string[] wordss,int startindex)
        {
            string[] strs = new string[wordss.Length - startindex];
            for (int i = 0; i < strs.Length; i++)
            {
                strs[i] = wordss[startindex + i];
            }
            return strs;
        }
        #endregion        
    }
}
